import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'logic.dart';
import 'state.dart';

class PreviewPhotoPage extends StatelessWidget {
  final PreviewPhotoLogic logic = Get.put(PreviewPhotoLogic());
  final PreviewPhotoState state = Get.find<PreviewPhotoLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 32),
      width: double.infinity,
      height: double.infinity,
      color: Colors.black54,
      child: Hero(
        tag: state.msgId??"",
        child: GestureDetector(
          onTap: () {
            Get.back();
          },
          onScaleEnd: (detail) {
            ///缩放手势结束,则重置记录scale的值
            logic.lastScale = 1;
          },
          onScaleUpdate: (ScaleUpdateDetails detail) {
            logic.scaleDouble(detail.scale, detail.rotation);
          },
          onDoubleTap: () {
            if (state.scale < state.MAX_SCALE) {
              logic.scaleUp();
            } else {
              logic.reset();
            }
          },
          child: GetBuilder<PreviewPhotoLogic>(
            init: logic,
            builder: (controller) {
              ///这里优先使用本地图片
              return Transform.scale(
                scale: state.scale,
                child: Image(
                  image: FileImage(File(state.path??"")),
                  errorBuilder: (context, _, b) {
                    return Image.network(
                      state.imgUrl??"",
                      loadingBuilder: (context, widget, chunk) {
                        return chunk == null
                            ? widget
                            : Container(
                                width: double.infinity,
                                height: double.infinity,
                                alignment: Alignment.center,
                                child: CircularProgressIndicator(),
                              );
                      },
                    );
                  },
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}
